<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */

namespace app\adminapi\controller\v1\wf;

use app\adminapi\controller\AuthController;
use app\Request;
use lflow\core\services\ProcessServices;
use think\App;

class Process extends AuthController
{

    protected ProcessServices $service;

    public function __construct(App $app, ProcessServices $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 资源列表
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): \think\response\Json
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['type', ''],
            ['name', ''],
            ['display_name', ''],
            ['instance_url', ''],
            ['state', ''],
            ['version', ''],
            ['creator', ''],
        ]);
        return app('json')->success($this->service->getProcess($where));
    }

    /**
     * 获取指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     */
    public function read(string $id): \think\Response\Json
    {
        $ret = $this->service->get($id);
        if ($ret) {
            return app('json')->success($ret->toArray());
        } else {
            return app('json')->fail('参数错误');
        }
    }

    /**
     * 添加资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response\Json
     */
    public function save(Request $request): \think\Response\Json
    {
        $data = $this->request->post('content');
        if (empty($data)) {
            return app('json')->fail('参数错误');
        }
        if ($this->service->deploy(json_decode($data, 1), $request->adminId())) {
            return app('json')->success('操作成功');
        }
        return app('json')->fail('操作成功');
    }

    /**
     * 更新指定资源
     *
     * @param \app\Request $request
     * @param string       $id
     *
     * @return \think\Response\Json
     */
    public function update(Request $request, string $id): \think\Response\Json
    {
        if (!$id || !($this->service->get($id))) {
            return app('json')->fail(100026);
        }
        $data = $this->request->post('content');
        if ($this->service->redeploy($id, json_decode($data, 1))) {
            return app('json')->success('操作成功');
        }
        return app('json')->fail('操作成功');
    }

    /**
     * 设置状态
     *
     * @param string     $id
     * @param string|int $status
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setStatus(string $id, string|int $status): \think\Response\Json
    {
        $this->service->updateState($id, $status);
        return app('json')->success('操作成功');
    }

    /**
     * 删除指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(string $id): \think\Response\Json
    {
        if (!$id) return app('json')->fail(100100);
        $this->service->cascadeRemove($id);
        return app('json')->success('删除成功');
    }
}
